package com.chris.design.pattern.samples.observer.jdk;

import java.util.Objects;
import java.util.Observable;

/**
 * 被观察者对象
 * @author Chris
 */
public class Watched extends Observable {
    private String data;

    public String getData() {
        return data;
    }

    @Override
    public synchronized int countObservers() {
        return super.countObservers();
    }

    public void setData(String data) {
        System.out.println("被观察者对象状态旧值:" + this.data);
        // 不等就修改
        if (!Objects.equals(this.data, data)) {
            this.data = data;
            setChanged();
        }
        System.out.println("被观察者对象状态新值:" + this.data);
        notifyObservers(this.data);
    }
}
